-- TODO почистить и отсортировать
local Color = require('libs/colorlibrary/colorlibrary')
--local Event = require('src/Event')
local Graphics = require('src/graphics')
local i18n = require('libs/i18n')
local Image = require('src/Graphics/Image')
local Label = require('src/Widgets/Label')
local Log = require('src/Log')
local State = require('src/State')

local BorderComponent = require('src/UI/Components/BorderComponent')
local MouseHoverComponent = require('src/UI/Components/MouseHoverComponent')
-- TODO remove unneded?
--local MovementComponent = require('src/UI/Components/MovementComponent')
local SnowComponent = require('src/UI/Components/SnowComponent')
local TextComponent = require('src/UI/Components/TextComponent')
local TextureComponent = require('src/UI/Components/TextureComponent')
local TweenComponent = require('src/UI/Components/TweenComponent')
local UI = require('src/UI/UI')
local UIInputSystem = require('src/UI/Systems/InputSystem')
-- TODO remove unneded?
--local UIMovementSystem = require('src/UI/Systems/MovementSystem')
local UIRenderSystem = require('src/UI/Systems/RenderSystem')
local UISnowSystem = require('src/UI/Systems/SnowSystem')
local UITweenSystem = require('src/UI/Systems/TweenSystem')

--local MainMenuState = {}
-- TODO как-то переоформить это?
local MainMenuState = State:derive('MainMenuState')

-- TODO как-то переоформить это?
MainMenuState.music = 'music/alexeevdv_-_lost_page.ogg'

-- ----------
-- menu --

function MainMenuState.click(button)
    if button.text == 'Start' then
        MainMenuState:startGame()
    end
    if button.text == 'Options' then
        MainMenuState:options()
    end
    if button.text == 'About' then
        MainMenuState:about()
    end
    if button.text == 'Quit' then
        MainMenuState:quitGame()
    end
end

-- Start Game
function MainMenuState:startGame()
    Log:info('Starting the game...')
    ENGINE.SM:Pop('MainMenuState')
    -- TODO сделать состояние "Новая игра", "Загрузить игру" и пр.
    ENGINE.CORE:init()
    ENGINE.CORE:initWinter()
end


-- Options
function MainMenuState:options()
    Log:info('Switching to options...')
--    ENGINE.SM:switch('options')
end

-- Options
function MainMenuState:about()
    Log:info('Switching to about...')
--    ENGINE.SM:switch('about')
end


function MainMenuState:quitGame()
    -- TODO перенацелить на функцию завершения программы
    Log:info('«Winter Anathema» ending... Bye!')
    love.event.quit(0)
end

-- ----------

-- state --
function MainMenuState:new()
    -- screen dimensions
    local screenW, screenH = love.window.getMode()

--     -- TODO переделать на более общий шрифт?
--    self.FontWidth = g_fontMedium:getWidth('.')
--    self.FontHeight = g_fontMedium:getHeight('.')

    self.UIRenderSystem = UIRenderSystem()
    self.UIInputSystem = UIInputSystem()
-- TODO remove unneded?
--    self.UIMovementSystem = UIMovementSystem()
    self.UI = {}

    -- background
    local bgImage = Image('MainMenuBackground')
    local bg = UI('MainMenuBackground', 0, 0, screenW, screenH)
    -- background texture component
    local bgTextureComp = TextureComponent('MainMenuBackground', bgImage.texture, 0, 0, screenW, screenH, 'center', 'bottom', true, 0.5)
    bg:addComponent(bgTextureComp)
    -- adding bg to UI
    table.insert(self.UI, bg)

    -- project name
    local prj = UI('ProjectName', 0, 0, screenW, screenH)
    local prjNameComp = TextComponent('ProjectName', 50, 80, nil, nil, i18n.translate('ProjectName'), g_fontLarge, Color.black, 'left')
    prj:addComponent(prjNameComp)
    -- adding project name to UI
    table.insert(self.UI, prj)

    -- snow
    local snow = SnowComponent(3000)
    bg:addComponent(snow)

    -- menu
    -- cover
    local coverImage = Image('OldRustyMetal')
    local coverW = coverImage.w
    local coverH = coverImage.h
    local cover = UI('MainMenuCover', screenW - coverW - 50, 50 - 700, coverW, coverH)
    -- cover texture component
    local coverTextureComp = TextureComponent('MainMenuCover', coverImage.texture, 0, 0, nil, nil, 'center', 'center', true, 0.5)
    cover:addComponent(coverTextureComp)
    -- adding cover to UI
    table.insert(self.UI, cover)

    -- TODO temporary!
    -- cover height = 450 / 5 steps (4 menu items + 1 period) = 90
    local step = 90
    local disp = 20

    -- button highlight image
    local btnHighImg = Image('PaintStroke')

    -- button Start Game
    local buttonStartGame = UI('ButtonStartGame', 0, disp + step * 0, coverW, step)
    local btnStartMouseHoverComp = MouseHoverComponent()
    -- button Start texture
    local btnStartTextureComp = TextureComponent('BtnStartTextureHigh', btnHighImg.texture, 0, 0, coverW, nil, 'center', 'bottom', true)
    buttonStartGame:addComponent(btnStartTextureComp)
    table.insert(btnStartMouseHoverComp.mouseOverIn, 'BtnStartTextureHigh')
    -- button Start text
    local btnStartTextNorm = TextComponent('BtnStartTextNorm', 0, 10, coverW, nil, i18n.translate('StartGame'), g_fontMedium, Color.black, 'center', 'top')
    local btnStartTextHigh = TextComponent('BtnStartTextHigh', 0, 10, coverW, nil, i18n.translate('StartGame'), g_fontMedium, Color.white, 'center', 'top')
    buttonStartGame:addComponent(btnStartTextNorm)
    buttonStartGame:addComponent(btnStartTextHigh)
    table.insert(btnStartMouseHoverComp.mouseOverIn, 'BtnStartTextHigh')
    table.insert(btnStartMouseHoverComp.mouseOverOut, 'BtnStartTextNorm')
    buttonStartGame:addComponent(btnStartMouseHoverComp)
    cover:addChild(buttonStartGame)

    -- button Options
    local buttonOptions = UI('ButtonOptions', 0, disp + step * 1, coverW, step)
    local btnOptionsMouseHoverComp = MouseHoverComponent()
    -- button Options texture
    local btnOptionsTextureComp = TextureComponent('BtnOptionsTextureHigh', btnHighImg.texture, 0, 0, coverW, nil, 'center', 'bottom', true)
    buttonOptions:addComponent(btnOptionsTextureComp)
    table.insert(btnOptionsMouseHoverComp.mouseOverIn, 'BtnOptionsTextureHigh')
    -- button Options text
    local btnOptTextNorm = TextComponent('BtnOptionsTextNorm', 0, 10, coverW, nil, i18n.translate('Options'), g_fontMedium, Color.black, 'center', 'top')
    local btnOptTextHigh = TextComponent('BtnOptionsTextHigh', 0, 10, coverW, nil, i18n.translate('Options'), g_fontMedium, Color.white, 'center', 'top')
    buttonOptions:addComponent(btnOptTextNorm)
    buttonOptions:addComponent(btnOptTextHigh)
    table.insert(btnOptionsMouseHoverComp.mouseOverIn, 'BtnOptionsTextHigh')
    table.insert(btnOptionsMouseHoverComp.mouseOverOut, 'BtnOptionsTextNorm')
    buttonOptions:addComponent(btnOptionsMouseHoverComp)
    cover:addChild(buttonOptions)

    -- button About
    local buttonAbout = UI('ButtonAbout', 0, disp + step * 2, coverW, step)
    local btnAboutMouseHoverComp = MouseHoverComponent()
    -- button About texture
    local btnAboutTextureComp = TextureComponent('BtnAboutTextureHigh', btnHighImg.texture, 0, 0, coverW, nil, 'center', 'bottom', true)
    buttonAbout:addComponent(btnAboutTextureComp)
    table.insert(btnAboutMouseHoverComp.mouseOverIn, 'BtnAboutTextureHigh')
    -- button About text
    local btnAboutTextNorm = TextComponent('BtnAboutTextNorm', 0, 10, coverW, nil, i18n.translate('AboutProject'), g_fontMedium, Color.black, 'center', 'top')
    local btnAboutTextHigh = TextComponent('BtnAboutTextHigh', 0, 10, coverW, nil, i18n.translate('AboutProject'), g_fontMedium, Color.white, 'center', 'top')
    buttonAbout:addComponent(btnAboutTextNorm)
    buttonAbout:addComponent(btnAboutTextHigh)
    table.insert(btnAboutMouseHoverComp.mouseOverIn, 'BtnAboutTextHigh')
    table.insert(btnAboutMouseHoverComp.mouseOverOut, 'BtnAboutTextNorm')
    buttonAbout:addComponent(btnAboutMouseHoverComp)
    cover:addChild(buttonAbout)

    -- button Quit
    local buttonQuit = UI('ButtonQuit', 0, disp + step * 3, coverW, step)
    local btnQuitMouseHoverComp = MouseHoverComponent()
    -- button Quit texture
    local btnQuitTextureComp = TextureComponent('BtnQuitTextureHigh', btnHighImg.texture, 0, 0, coverW, nil, 'center', 'bottom', true)
    buttonQuit:addComponent(btnQuitTextureComp)
    table.insert(btnQuitMouseHoverComp.mouseOverIn, 'BtnQuitTextureHigh')
    -- button Quit text
    local btnQuitTextNorm = TextComponent('BtnQuitTextNorm', 0, 10, coverW, nil, i18n.translate('QuitGame'), g_fontMedium, Color.black, 'center', 'top')
    local btnQuitTextHigh = TextComponent('BtnQuitTextHigh', 0, 10, coverW, nil, i18n.translate('QuitGame'), g_fontMedium, Color.white, 'center', 'top')
    buttonQuit:addComponent(btnQuitTextNorm)
    buttonQuit:addComponent(btnQuitTextHigh)
    table.insert(btnQuitMouseHoverComp.mouseOverIn, 'BtnQuitTextHigh')
    table.insert(btnQuitMouseHoverComp.mouseOverOut, 'BtnQuitTextNorm')
    buttonQuit:addComponent(btnQuitMouseHoverComp)
    cover:addChild(buttonQuit)

-- TODO movement vs tween?
    -- movement
--    local coverMoveComp = MovementComponent('CoverMovementComponent', 0, 2000)
--    cover:addComponent(coverMoveComp)

    -- tween movement
    local coverTweenComp = TweenComponent('CoverTweenComponent', 2, cover, {y = 80}, 'outQuart')
--    local coverTweenComp = TweenComponent('CoverTweenComponent', 2, coverMoveComp, {speedY = 0}, 'outQuart')
    cover:addComponent(coverTweenComp)
end

function MainMenuState:load()
    -- events
--    ENGINE.EVENTS:hook('onBtnClick', self.click)

--    if Config.PlayMusic then
--        --self.bgMusicInstance = self.bgMusic:play()
--        self.bgMusic:play()
--    end

    -- sounds
--    self.soundMove = love.audio.newSource('sounds/pick.ogg', 'static')
--    self.soundAccept = love.audio.newSource('sounds/accept.ogg', 'static')

    -- sounds setup for menu
--    menuMain:setSndMove(self.soundMove)
--    menuMain:setSndSuccess(self.soundAccept)
end


function MainMenuState:update(dt)
--    ENGINE.SM:update(dt)
--    menuMain:update()
--    if ENGINE.EVENTS.events['f10'] then
--        ENGINE.EVENTS.events['f10'] = nil
--        -- TODO общий выход
--        love.event.quit(0)
--    end
    UIInputSystem:update(self.UI, dt)
    UISnowSystem:update(self.UI, dt)
    UIMovementSystem:update(self.UI, dt)
    UITweenSystem:update(self.UI, dt)
end


function MainMenuState:draw()
    -- TODO разобраться почему если удаляешь эти две строки, переходит на intro!
    love.graphics.setColor(1, 1, 1, 1)
    love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight())

    UIRenderSystem:draw(self.UI)

    Graphics:ShowFPS()
end


function MainMenuState:exit()
--    ENGINE.EVENTS:unhook('onBtnClick', self.click)
end

-- ----------

return MainMenuState
